package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.Result;
import com.example.reggie.domain.Category;
import com.example.reggie.domain.Dish;
import com.example.reggie.domain.DishFlavor;
import com.example.reggie.domain.Setmeal;
import com.example.reggie.dto.DishDto;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.DeleteProvider;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishFlavorService dishFlavorService;

    // 菜品分页查询
    @GetMapping("/page")
    Result<Page> dishPageList(Integer page,Integer pageSize,String name){
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> dishList = dishPage.getRecords();
        log.info("dishList.size() = {}",dishList.size());
        List<DishDto> dishDtoList = new ArrayList<>(dishList.size());
        log.info("dishDtoList.size() = {}",dishDtoList.size());
        dishDtoPage.setRecords(dishDtoList);
        for (int i=0;i<dishList.size();i++){
            log.info(" i = {}",i);
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dishList.get(i),dishDto);
            dishDtoPage.getRecords().add(dishDto);
            String categoryName = categoryService.getById(dishList.get(i).getCategoryId()).getName();
            if (categoryName!=null){
                dishDtoPage.getRecords().get(i).setCategoryName(categoryName);
            }
        }


        return Result.success(dishDtoPage);
    }

    // 根据id查询菜品信息
    @GetMapping("/{id}")
    Result<DishDto> dishGetById(@PathVariable Long id){
        DishDto dishDto = new DishDto();
        Dish dish = dishService.getById(id);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return Result.success(dishDto);
    }


    // 根据分类id查询菜品信息
    @Cacheable(value = "dishCache",key = "#categoryId")
    @GetMapping("/list")
    public Result<List> dishList(Long categoryId){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        queryWrapper.eq(Dish::getStatus,1);
        List<Dish> dishs = dishService.list(queryWrapper);
        List<DishDto> list = new ArrayList<>();
        for (int i = 0; i < dishs.size(); i++) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dishs.get(i),dishDto);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishs.get(i).getId());
            List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(flavors);
            list.add(dishDto);
        }
        return Result.success(list);
    }


    // 批量更改菜品状态
    @CacheEvict(value = "dishCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result<String> dishStatusChange(@PathVariable int status,@RequestParam("ids") Long[] ids){
        Dish dish = new Dish();
        dish.setStatus(status);
        for (int i=0;i< ids.length;i++){
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getId,ids[i]);
            dishService.update(dish,queryWrapper);
        }
        return Result.success("修改成功");
    }

    // 添加菜品
    @CacheEvict(value = "dishCache",allEntries = true)
    @PostMapping
    public Result<String> dishAdd(@RequestBody DishDto dishDto){
        dishService.dishAdd(dishDto);
        return Result.success("添加成功");
    }

    //批量删除菜品
    @CacheEvict(value = "dishCache",allEntries = true)
    @DeleteMapping
    public Result<String> dishDelete(@RequestParam("ids") Long[] ids){
        dishService.removeByIds(Arrays.asList(ids));
        for (int i=0;i<ids.length;i++){
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,ids[i]);
            dishFlavorService.remove(queryWrapper);
        }

        return Result.success("删除成功");
    }


    // 修改菜品信息
    @PutMapping
    @CacheEvict(value = "dishCache",allEntries = true)
    public Result<String> dishUpdate(@RequestBody DishDto dishDto){
        dishService.dishUpdate(dishDto);

        return Result.success("修改成功");
    }
}
